package com.example.baseadapter;

import android.content.Intent;
import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.os.Bundle;
import android.support.v7.app.ActionBarActivity;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.widget.EditText;
import android.widget.Toast;

public class EditCase extends ActionBarActivity{
	int caseId;
	DatabaseHelper db;
	EditText plainEdit,resEdit,caserefEdit,noteEdit,courtEdit;
	@Override
	public void onCreate(Bundle savedInstanceState) {
		
		super.onCreate(savedInstanceState);
		
		db = new DatabaseHelper(getApplicationContext());
		
		setContentView(R.layout.edit_case);
		
		getSupportActionBar().setBackgroundDrawable(new ColorDrawable(Color.rgb(10,132,132)));
		getSupportActionBar().setDisplayHomeAsUpEnabled(true);
		getSupportActionBar().setHomeButtonEnabled(true);
		plainEdit=(EditText)findViewById(R.id.plainEdit);
		resEdit=(EditText)findViewById(R.id.resEdit);
		caserefEdit=(EditText)findViewById(R.id.caserefEdit);
		noteEdit=(EditText)findViewById(R.id.noteEdit);
		courtEdit=(EditText)findViewById(R.id.courtEdit);
		caseId = getIntent().getExtras().getInt("caseId");
		
		//setting case data in edittext
		Case obj = db.getSingleCase(caseId );
		plainEdit.setText(obj.getPlaintiff());
		resEdit.setText(obj.getRespondent());
		caserefEdit.setText(obj.getCaseRefno());
		noteEdit.setText(obj.getCaseDetail());
		courtEdit.setText(obj.getCourtDetail());

		Toast.makeText(this,plainEdit.getHint(), Toast.LENGTH_LONG).show();
	}
	
	
	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		MenuInflater inflater = getMenuInflater();
		inflater.inflate(R.menu.editcase_menu, menu);

		return super.onCreateOptionsMenu(menu);
	}

	/**
	 * On selecting action bar icons
	 * */
	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		
		
		    switch (item.getItemId()) {
		        case android.R.id.home:
		            Intent h = new Intent(this,CaseDetail.class);
		            h.putExtra("id", caseId);
		            startActivity(h);
		            return true;
		  
		// Take appropriate action for each action item click
	
		case R.id.action_update:
			// adding new icon in the action bar
			updateCase();
			return true;

		default:
			return super.onOptionsItemSelected(item);
		}

	}
	
	public void updateCase() {
	  //Toast.makeText(this,"Hello Sameer",Toast.LENGTH_LONG).show();	
	  Case caseObj = new Case();
	  caseObj.setCaseId(caseId);
	  caseObj.setPlaintiff(plainEdit.getText().toString());
	  caseObj.setRespondent(resEdit.getText().toString());
	  caseObj.setCaseRefno(caserefEdit.getText().toString());
	  caseObj.setCaseDetail(noteEdit.getText().toString());
      caseObj.setCourtDetail(courtEdit.getText().toString());
      //it will return 1 ,if statement execute successfully then value of x will be 1.
	  int x=db.updateCase(caseObj);
	  String s=String.valueOf(x);
	  Toast.makeText(this,"Updated  "+s,Toast.LENGTH_LONG).show();
		
	}
	
	@Override
	public void onBackPressed()
	{
		Intent i=new Intent(this,CaseDetail.class);
		i.putExtra("id",caseId);
		startActivity(i);
	}

}
